package com.server.questionbank.snframe.controller.Login;

import com.server.questionbank.snframe.controller.Login.entity.RegisterData;
import com.server.questionbank.snframe.controller.Login.entity.UpdateUserData;
import com.server.questionbank.snframe.domain.Course.Result.CourseFigures;
import com.server.questionbank.snframe.domain.Course.TCourse;
import com.server.questionbank.snframe.domain.ResDTO.ResponseResult;
import com.server.questionbank.snframe.domain.User.TUser;
import com.server.questionbank.snframe.service.UsersService.UsersService;
import com.server.questionbank.snframe.util.MinIo.MinIoBucket;
import com.server.questionbank.snframe.util.MinIo.MinIoFactory;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    UsersService usersService;
    @Autowired
    MinIoFactory minIoFactory;

    // 获取登录用户信息
    @PostMapping("/getLoginUser")
    public ResponseResult<TUser> getLoginUser() {
        TUser user = usersService.getLoginUser();
        if (user == null) return ResponseResult.error("未登录，请先登录");
        return ResponseResult.success(user);
    }

    // 更新头像
    @PostMapping("/updateAvatar")
    public ResponseResult<String> updateAvatar(@RequestBody UpdateUserData updateUserData) {
        TUser user = usersService.getLoginUser();
        if (user == null) return ResponseResult.error("未登录，请先登录");
        usersService.updateUserAvatar(user.getUserId(), updateUserData.getAvatar());
        return ResponseResult.success("更新头像成功");
    }


    // 获取我的课程
    @PostMapping("/getCreateCourse")
    public ResponseResult<List<CourseFigures>> getMyCourse()
    {
        TUser user = usersService.getLoginUser();
        if (user == null) return ResponseResult.error("用户未登录!");
        List<CourseFigures> myCourse = usersService.getCreateCourse(user.getUserId());
        if (myCourse == null) return ResponseResult.error("获取课程失败!");
        return ResponseResult.success(myCourse);
    }

    // 获取我加入的课程
    @PostMapping("/getJoinCourse")
    public ResponseResult<List<CourseFigures>> getJoinCourse()
    {
        TUser user = usersService.getLoginUser();
        if (user == null) return ResponseResult.error("用户未登录!");
        List<CourseFigures> joinCourse = usersService.getJoinCourse(user.getUserId());
        if (joinCourse == null) return ResponseResult.error("获取课程失败!");
        return ResponseResult.success(joinCourse);
    }

    // 获取 我的收藏的课程
    @PostMapping("/getCollectCourse")
    public ResponseResult<List<CourseFigures>> getCollectCourse()
    {
        TUser user = usersService.getLoginUser();
        if (user == null) return ResponseResult.error("用户未登录!");
        List<CourseFigures> collectCourse  = usersService.getCollectCourse(user.getUserId());
        if (collectCourse == null) return ResponseResult.error("获取课程失败!");
        return ResponseResult.success(collectCourse);
    }

}
